package com.ziven.common.widget.value;

import android.graphics.ColorFilter;
import android.graphics.PorterDuff;
import android.graphics.PorterDuffColorFilter;
import android.graphics.drawable.ColorDrawable;
import android.graphics.drawable.Drawable;
import android.view.View;

import androidx.annotation.NonNull;

import com.ziven.easygo.util.ResourceUtils;
import com.ziven.easygo.util.ViewHelper;

/**
 * @author Ziven
 */
public final class BgColor extends Value {

    public static BgColor filterColorId(int colorId) {
        return filterColorId(colorId, PorterDuff.Mode.DARKEN);
    }

    public static BgColor filterColor(int color) {
        return filterColor(color, PorterDuff.Mode.DARKEN);
    }

    public static BgColor filterColorId(int colorId, PorterDuff.Mode mode) {
        return filterColor(ResourceUtils.getColor(colorId), mode);
    }

    public static BgColor filterColor(int color, PorterDuff.Mode mode) {
        return filter(new PorterDuffColorFilter(color, mode));
    }

    public static BgColor filter(@NonNull ColorFilter filter) {
        return new BgColor().accept(my -> my.value = filter);
    }

    private BgColor() {}

    @Override
    protected void takeEffect(@NonNull ViewHelper<? extends View> helper) {
        helper.getViewNulls().doNotNull(view -> value(ColorFilter.class, value -> {
            Drawable bg = view.getBackground() != null
                    ? view.getBackground()
                    : new ColorDrawable();
            bg.setColorFilter(value);
        }));
    }
}
